package leetcode.code0989;

import java.util.ArrayList;
import java.util.List;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;

public class Solution implements IDebug {

	public List<Integer> addToArrayForm(int[] num, int k) {
		List<Integer> ans = new ArrayList<>();

		int len = num.length;
		int p = len - 1;
		int carry = 0;
		while (k > 0 && p >= 0) {
			int last = k % 10;
			int sum = carry + num[p--] + last;
			if (sum < 10) {
				ans.add(0, sum);
				carry = 0;
			} else {
				carry = 1;
				ans.add(0, sum % 10);
			}
			k /= 10;
		}
		while (p >= 0) {
			if (carry == 0) {
				ans.add(0, num[p--]);
			} else {
				int sum = carry + num[p--];
				if (sum < 10) {
					ans.add(0, sum);
					carry = 0;
				} else {
					carry = 1;
					ans.add(0, sum % 10);
				}
			}
		}
		while (k > 0) {
			int last = k % 10;
			int sum = carry + last;
			if (sum < 10) {
				ans.add(0, sum);
				carry = 0;
			} else {
				carry = 1;
				ans.add(0, sum % 10);
			}
			k /= 10;
		}
		if (carry > 0) {
			ans.add(0, carry);
		}
		return ans;
	}

	@Override
	public void debug4() {
		List<Integer> ans = HelpDebug.str2intlist("[1,2,3,4]");
		HelpDebug.compare(ans,this.addToArrayForm(HelpDebug.str2array3("[1,2,0,0]"), 34));

	}

	@Override
	public void debug3() {
		List<Integer> ans = HelpDebug.str2intlist("[4,5,5]");
		HelpDebug.compare(ans,this.addToArrayForm(HelpDebug.str2array3("[2,7,4]"), 181));

	}

	@Override
	public void debug2() {
		List<Integer> ans = HelpDebug.str2intlist("[1,0,2,1]");
		HelpDebug.compare(ans,this.addToArrayForm(HelpDebug.str2array3("[2,1,5]"), 806));

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();


	}

}
